/* eslint-disable no-unused-vars */
// 引入 vue  和 vue-router
import Vue from 'vue'
import Router from 'vue-router'
// 引入store
// import store from '../store'
// 引入页面组件
import Login from '@/views/login'
import Home from '@/views/home'
import registered from '@/views/registered'
import Cart from '@/views/cart'
import Category from '@/views/category'
import MyPage from '@/views/myPage'
import AllCategory from '@/views/category/allCategory'
import CategoryList from '@/views/category/category-list'
import ListPopularity from '@/views/category/list-popularity'
import ListPrice from '@/views/category/list-price'
import ListScreen from '@/views/category/list-screen'

import Registered from '@/views/registered'
import Cart from '@/views/cart'
import Category from '@/views/category'
import Agreement from '@/views/agreement'
import Pay from '@/views/pay'
import Address from '@/views/address'
import Consignee from '@/views/consignee'

import News from '@/views/myPage/news.vue'
import Header from '@/components/navBar.vue'
import setup from '@/views/myPage/setup.vue'
import myorder from '@/views/myPage/myorder.vue'
// 注册插件
Vue.use(Router)

// Redirected when going from "/login" to "/home" via a navigation guard.
// 对Router 中push 方法进行重写
const pushPlugin = Router.prototype.push
Router.prototype.push = function (paramas) {
  pushPlugin.call(this, paramas).catch(e => {
    console.log('屏蔽这个错误')
  })
}

// 实例路由
const router = new Router({
  // 配置路由
  mode: 'history',
  routes: [
    {
      path: '/',
      redirect: '/login'
    }, // 重定向  默认地址
    {
      path: '/login',
      component: Login,
      meta: { nologin: true }
    },
    {
      path: '/home',
      component: Home
    },
    {
      path: '/registered',
      component: registered
    },
    {
      path: '/cart',
      component: Cart
    },
    {
      path: '/category',
      component: Category
    },
    {
      path: '/allCategory',
      component: AllCategory
      // meta: {
      //   title: '子分类'
      // }
    },
    {
      path: '/myPage',
      component: MyPage
    },
    { path: '/', redirect: '/login' }, // 重定向  默认地址
    { path: '/login', component: Login, meta: { nologin: true } },
    { path: '/home', component: Home },
    { path: '/registered', component: registered },
    { path: '/cart', component: Cart },
    { path: '/category', component: Category },
    {
      path: '/allCategory',
      component: AllCategory
    },
    {
      path: '/categorylist',
      component: CategoryList,
      children: [
        {
          name: 'popularity',
          path: '/listPopularity',
          component: ListPopularity
        },
        {
          name: 'price',
          path: '/listprice',
          component: ListPrice
        },
        {
          name: 'screen',
          path: '/listscreen',
          component: ListScreen
        }
      ]
    },
    { path: '/myPage', component: MyPage },
    {
      path: '/myPage/news',
      components: { default: News, header: Header },
      meta: {
        title: '消息中心'
      }
    },
    {
      path: '/myPage/setup',
      components: { default: setup, header: Header },
      meta: {
        title: '系统设置'
      }
    },
    {
      path: '/myPage/myorder',
      components: { default: myorder, header: Header },
      meta: {
        title: '我的订单'
      }
    }

  ]

})
// 路由拦截 全局
router.beforeEach(function (to, from, next) {
  if (to.meta.nologin) {
    next()
    return
  }
  // 将 数据存入 vuex中
  // const token = store.getters['common/getToken']
  const token = 'fasjdfkl;ai'
  // console.log(token)
  if (token) {
    next()
  } else {
    next('/login')
  }
})
export default router
